<?php


function cetak_laporan(){
  
echo '
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>HRIS Krakatau Medika :: Printed out report</title> 
</head>

<body style="font:10px verdana;" onload = "javascript:breakeveryheader()"><pre>';
	$periode_id = isset($_GET['periode_id']) ? (int)$_GET['periode_id']: get_periode_gaji();
	$periodes = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periodes['tanggal']));
	$periode=returnDate($bulan, "month").' '.$periodes['tahun'] ;	
	$periode_filename =returnDate($bulan, "month").' '.$periodes['tahun'] ;
 
	$end=get_batas_tmt() ;
	
	$page=1;
	$total_line_per_page=3;
	cetak_list_page($page, $periode) ;
	$apos = $akomponen = $aregister = $atotal = array();
	$i=3;  //1=Gaji KM  2=Gaji Dibayarkan 3=Gaji KS 
	$header = array(
		' '=>array('style'=>'border-bottom:2px solid;width:100%'),  
	);	
	$teks_komponen=sprintf('%03d',1).str_pad(' = Gaji KM',36).
		sprintf('%03d',2).str_pad(' = Gaji Dibayarkan',36).
		sprintf('%03d',3).str_pad(' = Gaji KS',36);
	$query = "SELECT komponen_gaji_id,nama_komponen_gaji FROM pr_komponen_gaji ORDER BY tipe , komponen_gaji_id ASC";
	$result = my_query($query) ;
	$jumlah_komponen=my_num_rows($result)+4 ;

	echo '____________________________________________________________________________________________________________________________________________________________________________________________________________'."\n";
	while( $r = my_fetch_array($result) ){
		$i++;
		$apos[$r['komponen_gaji_id']]=$i ;
		$teks_komponen.=sprintf('%03d',$i).str_pad(' = '.$r['nama_komponen_gaji'],36) ;
		if($i%5==0){
			echo $teks_komponen."\n" ;
			$teks_komponen='';
		}	
	}
	if($i%5==0){	
		echo $teks_komponen."\n" ;
	} 
	echo sprintf('%03d',100).str_pad(' = Take home pay',36) ;
	//echo $teks_komponen."\n" ;
	echo "\n" ;
	
	cetak_list_header($jumlah_komponen) ; 
	
	$query = "   select a.karyawan_id FROM pr_gaji a INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
		 WHERE a.periode_id={$periode_id}  ORDER BY b.nik ASC ";	
	$result = my_query($query) ;
	$no_karyawan=$no_line=0;
	while($ey = my_fetch_array($result )){
		$no_karyawan++;
		$no_line++;
		$karyawan = loaddata_karyawan($ey['karyawan_id']);  
		$jabatan =  loaddata_jabatan($karyawan['jabatan_id']);  
		
		$query = " SELECT gapok_km,gaji_dibayar,gapok_ks,takehomepay FROM pr_gaji 
			WHERE periode_id={$periode_id}
			AND karyawan_id=".$ey['karyawan_id']
		;
		$pr_gajis = my_fetch_array(my_query($query)) ;

		$akomponen=array() ;
		$akomponen[1]=$pr_gajis['gapok_km'] ;
		$akomponen[2]=$pr_gajis['gaji_dibayar'] ;
		$akomponen[3]=$pr_gajis['gapok_ks'] ;
		$akomponen[100]=$pr_gajis['takehomepay'] ;
		
		$query = "SELECT komponen_id, kalkulasi FROM pr_gaji_komponen 
			WHERE karyawan_id={$ey['karyawan_id']} 
			AND periode_id ={$periode_id}"; 
		$resd = my_query($query);
		
		while($eyd = my_fetch_array($resd)){
			$akomponen[$apos[$eyd['komponen_id']]]=$eyd['kalkulasi'] ;
		}	
		
		$aregister[1]=sprintf('%03d',$no_karyawan).'   '.str_pad($karyawan['nik'].'   '.$jabatan['nomor_posisi'].'/'.$jabatan['pos_biaya'],38).' ' ;
		$aregister[2]='      '.str_pad($karyawan['nama_gelar'],38).' ' ;
		$aregister[3]='      '.str_pad(substr($jabatan['nama_jabatan'],0,35),38).' ' ;
		$aregister[4]='      '.str_pad(date('d-m-Y',strtotime($karyawan['tmb'] ) ) .' / '.$karyawan['ptkp_status'].$karyawan['ptkp_tanggung'],38).' ' ;
		$nobaris=$ndata2 =$nopos=1;
		$tex = max($jumlah_komponen,12*5);
		for($cnt=1; $cnt <=$tex; $cnt+=12){
			if($nobaris<=4){
				$teks_komponen=$aregister[$nobaris] ;
				$nobaris++;
			}else{
				$teks_komponen='      '.str_pad(' ',38).' ' ;
			}
			for($kolom=1; $kolom <=12; $kolom++){
				if($nopos<=$jumlah_komponen){
					$atotal_per_page[$nopos]+=$akomponen[$nopos] ;
					$atotal[$nopos]+=$akomponen[$nopos] ;
					$teks_komponen.=str_pad(rp_format($akomponen[$nopos]),12," ",STR_PAD_LEFT).' ' ;
					$nopos++;
				}else{	
					$teks_komponen.=str_pad(' ',12).' ' ;
				}	
				if($ndata2 == $tex ){
					$atotal_per_page[100]+=$akomponen[100] ;
					$teks_komponen.= str_pad(rp_format($akomponen[100]),12," ",STR_PAD_LEFT)."\n" ;	
					$atotal[100]+=$akomponen[100] ;
				}
				$ndata2++;
			}
			echo $teks_komponen. "\n" ;	
			
		}
		if($no_line >= $total_line_per_page ){
			$total_line_per_page=5 ;
			$no_line=0;
			list_footer($atotal_per_page, false ) ;
			//echo chr(12);
			echo '<P></P>';
			$page++;
			cetak_list_page($page, $periode) ;
			cetak_list_header($jumlah_komponen) ;
			$atotal_per_page=array();
		}
		
	}
	//halaman terakhir
	//echo chr(12);
	echo '<P></P>';
	$page++;
	cetak_list_page($page, $periode) ;
	cetak_list_header($jumlah_komponen) ;	
	cetak_list_footer($atotal, true ) ;

	$ttd_person_1=MAN_DEPARTEMEN_HEAD_OFFICE_NAME ;
	$ttd_jabat_1=MAN_DEPARTEMEN_HEAD_OFFICE ;
	$ttd_person_2=MAN_DEPARTEMEN_MANAGER_NAME ;
	$ttd_jabat_2=MAN_DEPARTEMEN_MANAGER ;
	
	echo '____________________________________________________________________________________________________________________________________________________________________________________________________________'."\n";
	echo ''."\n";
	echo ''."\n";
	echo '                        Cilegon,      '.$periode."\n";
	echo ''."\n";
	echo '                            Di Keluarkan oleh,                          Di Setujui oleh,'."\n";
	echo ''."\n";
	echo ''."\n";
	echo ''."\n";
	echo ''."\n";
	echo '                        ('.str_pad($ttd_person_1,30," ",STR_PAD_BOTH).')        ('.str_pad($ttd_person_2,30," ",STR_PAD_BOTH).')'."\n";
							    //123456789012345678901234567890          123456789012345678901234567890
	echo '                        ________________________________        ________________________________'."\n";
	echo '                        '.str_pad($ttd_jabat_1,32," ",STR_PAD_BOTH).'        '.str_pad($ttd_jabat_2,32," ",STR_PAD_BOTH)."\n";
	//echo chr(12);
	
	echo'
	
<script>
function breakeveryheader(){
if (!document.getElementById){
	alert("You need IE5 or NS6 to run this example")
	return
}
var thestyle=  "always" ;
for (i=0; i<document.getElementsByTagName("P").length; i++)
	document.getElementsByTagName("P")[i].style.pageBreakBefore=thestyle
}
//window.print();
</script>   
</body> 
</html>';
exit;	
}


function cetak_list_page($page, $periode){
 echo MAN_CORPORATE_NAME ."\n";
 echo str_pad(MAN_DEPARTEMEN_HR_GA,195).'Hal : '.str_pad( sprintf('%03d',$page),4)."\n";
 echo '                                                                                   Data Rincian Pembayaran Gaji Karyawan'."\n";
 echo '                                                                                             PERIODE '.$periode."\n";
 echo '                                                                                                                                                                                               Urut : N.I.K.'."\n";
 }

function cetak_list_header($jumlah_komponen){

	$aregister[1]=str_pad('      Register       / No. Posisi  / CCC',45) ;
	$aregister[2]=str_pad('      Nama Kayawan',45) ;
	$aregister[3]=str_pad('  No. Jabatan',45) ;
	$aregister[4]=str_pad('      T M B          / Mar. Sts.',45) ;
	$nobaris=$ndata=$nopos=1;
	echo '____________________________________________________________________________________________________________________________________________________________________________________________________________'."\n";
	$tex = max($jumlah_komponen,12*5);
	
	for($cnt=1; $cnt <=$tex; $cnt+=12){
		if($nobaris<=4){
			$teks_komponen=$aregister[$nobaris] ;
			$nobaris++;
		}else{
			$teks_komponen=str_pad(' ',45) ;
		}
		for($kolom=1; $kolom <=12; $kolom++){
			if($nopos<=$jumlah_komponen){
				$teks_komponen.=str_pad(sprintf('%03d',$nopos),12," ",STR_PAD_LEFT).' ' ;
				$nopos++;
			}else{	
				$teks_komponen.=str_pad(' ',12).' ' ;
			}
			if($ndata == $tex ){
				$teks_komponen.= str_pad('100',12," ",STR_PAD_LEFT).' ' ;
			} 			
			$ndata++;
		}
		echo $teks_komponen."\n" ;	
		
	}
 	echo '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'."\n";
}	
	

function cetak_list_footer($atotal_per_page, $eof ){
	$jumlah_komponen=count($atotal_per_page);
	if($eof){
		$teks='PEMBAYARAN' ;
		$len_digit1=14 ;
		$len_digit2=23 ;
	}else{		
		$teks='HALAMAN' ;
		$len_digit1=12 ;
		$len_digit2=45 ;
		echo '                       _____________________________________________________________________________________________________________________________________________________________________________________'."\n";
	}	
	$nopos=$ndata3 =1 ;
	$tex=max($jumlah_komponen,12*5);
	for($cnt=1; $cnt <=$tex; $cnt+=12){
		if($cnt==1){
			$teks_komponen=str_pad('TOTAL '.$teks.' :',$len_digit2," ",STR_PAD_RIGHT);
		}else{	
			$teks_komponen=str_pad(' ',$len_digit2 );
		}
		for($kolom=1; $kolom <=12; $kolom++){
		
			if($nopos<=$jumlah_komponen){
				$teks_komponen.=str_pad(rp_format($atotal_per_page[$nopos]),$len_digit1," ",STR_PAD_LEFT).' ' ;
				$nopos++;
			}else{	
				$teks_komponen.=str_pad(' ',$len_digit1).' ' ;
			}	
			if($ndata3 == ($tex -1)){ 
				$teks_komponen.=str_pad(rp_format($atotal_per_page[100]),$len_digit1," ",STR_PAD_LEFT).' ' ;
			}
			$ndata3++;
		}
		echo $teks_komponen."\n" ; 			
	}
}	
 